<?php
session_start();

/**
 * Retorna el html de los registros de twitter requeridos.
 * 
 * @author Jonathan.Araque 09/Sep/2012
 * 
 * @param type $tipo Tipo de twitt
 * @param type $criterio Criterio de búsqueda
 * @param type $cantidad Cantidad de twitts 
 * @return string html de los registros
 */
function getTwitterHtml($tipo, $criterio, $cantidad) {

    $html = "\n<div class='slides_container'>";
    switch ($tipo) {
        case '1':
            $content = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=$criterio&count=$cantidad");
            $cant = 1;
            $xml = json_decode($content);
            foreach ($xml as $status) {
                $html .= getTwitterItemHtml(
                        $status->user->profile_image_url, 
                        $status->user->screen_name, 
                        $status->text,
                        $status->entities, 
                        $cant);
            }
            break;
        case '2':
            $content = file_get_contents("http://search.twitter.com/search.json?q=$criterio&rpp=$cantidad&include_entities=true&result_type=mixed&callback=?");
            $cant = 1;
            $xml = json_decode($content);
            foreach ($xml->results as $result) {
                $html .= getTwitterItemHtml(
                        $result->profile_image_url, 
                        $result->from_user, 
                        $result->text,
                        $status->entities, 
                        $cant);
            }
            break;
    }
    $html .= "\n</div>";

    return $html;
}

/**
 * Retorna el html correspondiente a cada item de twitter.
 * 
 * @author Jonathan.Araque 09/Sep/2012
 * 
 * @param type $profile_image_url Url del ávatar
 * @param type $screen_name Nombre del usuario
 * @param type $text Twitt
 * @param type $entities entidades
 * @param type $id Identificador del tweet dentro del listado
 * @return string
 */
function getTwitterItemHtml($profile_image_url, $screen_name, $text, $entities, $id) {
    $html = "\n<div class='slide'>";
    $html .= "\n<img src='$profile_image_url' style='float: left' />";
    $html .= "\n<h1>@$screen_name</h1>";
    $html .= "\n<p>$text</p>";
    $html .= getMediaFromEntities($entities, 'photo');
    $html .= "\n<a href='#$id' class='link'></a>";
    $html .= "\n</div>";
    return $html;
}

/**
 * Retorna el html correspondiente a todos los recursos multimedia de un tweet.
 * 
 * @author Jonathan.Araque 16/Oct/2012
 * 
 * @param type $entidades
 * @param type $tipo
 * @return string
 */
function getMediaFromEntities($entidades, $tipo) {
    $html = "";
    if ($entidades != null && isset($entidades->media)) {
        foreach ($entidades->media as $media) {
            if ($tipo == $media->type) {
                $html .= "\n<img src='$media->media_url' style='float: left' />";
            }
        }
    }
    return $html;
}

if ($_SESSION["slcTipo"] == null) {
    header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">

        <title>Twitter</title>

        <link rel="stylesheet" href="css/global.css">
	
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
	<script src="js/slides.min.jquery.js"></script>
	<script>
		$(function(){
                // Set starting slide to 1
                var startSlide = 1;
                // Get slide number if it exists
                if (window.location.hash) {
                    startSlide = window.location.hash.replace('#','');
                }
                // Initialize Slides
			$('#slides').slides({
                    preload: true,
				preloadImage: 'img/loading.gif',
                    generatePagination: true,
				play: 5000,
				pause: 2500,
                    hoverPause: true,
                    // Get the starting slide
                    start: startSlide,
                    animationComplete: function(current){
                        // Set the slide number as a hash
                        window.location.hash = '#' + current;
                    }
			});
		});
	</script>
    </head>
    <body>
        <div id="container">
            <div id="example">
                <div id="slides">
                    <?php echo getTwitterHtml($_SESSION["slcTipo"], $_SESSION["txtCriterio"], $_SESSION["txtCantidad"]); ?>
                    <a href="#" class="prev">
                        <img src="img/arrow-prev.png" width="24" height="43" alt="Arrow Prev">
                    </a>
                    <a href="#" class="next">
                        <img src="img/arrow-next.png" width="24" height="43" alt="Arrow Next">
                    </a>
                </div>
                <img src="img/example-frame.png" width="739" height="341" alt="Example Frame" id="frame">
            </div>
            <div id="footer">
                <p>For full instructions go to <a href="http://slidesjs.com" target="_blank">http://slidesjs.com</a>.</p>
                <p>Slider design by Orman Clark at <a href="http://www.premiumpixels.com/" target="_blank">Premium Pixels</a>. You can donwload the source PSD at <a href="http://www.premiumpixels.com/clean-simple-image-slider-psd/" target="_blank">Premium Pixels</a></p>
                <p>&copy; 2010 <a href="http://nathansearles.com" target="_blank">Nathan Searles</a>. All rights reserved. Slides is licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache license</a>.</p>
            </div>
        </div>
        <?php include('menu.html'); ?>
    </body>
</html>
